#include "liveviewmanager.h"
#include "liveinfoview.h"
#include "livestatusview.h"
#include "livewindow.h"
#include "usersettings.h"
#include <QShortcut>
#include <QTimer>
#include <QVBoxLayout>

LiveViewManager::LiveViewManager(QWidget* main_view, QObject* parent) : QObject(parent)
{
    window_ = new LiveWindow(main_view, Qt::Widget);
    window_->setGeometry(QRect(QPoint(-1300, 720), QSize(1280, 720)));

    new QShortcut(
            QKeySequence("ALT+CTRL+SHIFT+L"), main_view, window_,
            [this] {
                const auto pos = window_->pos();
                if (pos.x() < 0) {
                    window_->move(QPoint(400, 150));
                    window_->raise();
                } else {
                    const auto sz = window_->size();
                    window_->move(QPoint(-sz.width() - 20, sz.height()));
                }
            },
            Qt::WindowShortcut);

    LiveInfoView* info_view = new LiveInfoView(QString::fromLatin1("Live"), window_);
    info_view->resize(1280, 720);
    window_->addView(info_view, tr("LiveInfo"));
    info_views_.push_back(info_view);
    connect(info_view, &LiveInfoView::captchaStateChanged, this,
            &LiveViewManager::captchaStateChanged);

    int frequency = UserSettings::instance().recorderSettings().regular_monitor_frequency * 1000;
    int count = UserSettings::instance().recorderSettings().realtime_monitor_count;
    for (int i = 0; i < count; ++i) {
        const QString num = QString::number(i);
        auto view =
                new LiveStatusView(QString::fromLatin1("RtLiveStatus") + num, frequency, window_);
        view->resize(1280, 720);
        realtime_views_.push_back(view);
        window_->addView(view, tr("RtLiveStatus-") + num);
        connect(view, &LiveStatusView::captchaStateChanged, this,
                &LiveViewManager::captchaStateChanged);
    }

    count = UserSettings::instance().recorderSettings().regular_monitor_count;
    for (int i = 0; i < count; ++i) {
        const QString num = QString::number(i);
        auto view = new LiveStatusView(QString::fromLatin1("LiveStatus") + num, window_);
        view->resize(1280, 720);
        status_views_.push_back(view);
        window_->addView(view, tr("LiveStatus-") + num);
        connect(view, &LiveStatusView::captchaStateChanged, this,
                &LiveViewManager::captchaStateChanged);
    }

    QTimer::singleShot(500, this, [this] {
        if (status_views_.empty()) {
            status_views_.at(0)->getLiveStatus(0, yc::PlatformType::DOUYIN_PLATFORM_TYPE,
                                               QString("xmleijun"), 5000, LiveHandler());
        }
        window_->show();
    });
}

LiveViewManager::~LiveViewManager()
{
    delete window_;
}

bool LiveViewManager::infoPassedCaptcha() const
{
    for (auto p : info_views_) {
        if (!p->passedCaptcha()) {
            return false;
        }
    }
    return true;
}

bool LiveViewManager::passedCaptcha() const
{
    for (auto p : info_views_) {
        if (!p->passedCaptcha()) {
            return false;
        }
    }
    for (auto p : realtime_views_) {
        if (!p->passedCaptcha()) {
            return false;
        }
    }
    for (auto p : status_views_) {
        if (!p->passedCaptcha()) {
            return false;
        }
    }
    return true;
}

int LiveViewManager::needCaptchaCount() const
{
    int count = 0;
    for (auto p : info_views_) {
        if (!p->passedCaptcha()) {
            ++count;
        }
    }
    for (auto p : realtime_views_) {
        if (!p->passedCaptcha()) {
            ++count;
        }
    }
    for (auto p : status_views_) {
        if (!p->passedCaptcha()) {
            ++count;
        }
    }
    return count;
}
